package com.lfk.justweengine.utils.tool;


import com.lfk.justweengine.utils.tools.SpUtils;
import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;
import ohos.app.Context;
import org.junit.Assert;
import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SpUtilsOhosTest {

    @Test
    public void testGetForString() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        SpUtils.put(context, "Test1", "Test1");
        String str = (String) SpUtils.get(context, "Test1", "T");
        Assert.assertEquals("Test1", str);
    }

    @Test
    public void testGetForInteger() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        SpUtils.put(context, "Test2", new Integer("2"));
        Integer str = (Integer) SpUtils.get(context, "Test2", new Integer("1"));
        Assert.assertEquals("2", str.toString());
    }

    @Test
    public void testGetForBoolean() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        SpUtils.put(context, "Test3", true);
        Boolean str = (Boolean) SpUtils.get(context, "Test3", true);
        Assert.assertEquals("true", str.toString());
    }

    @Test
    public void testGetForFloat() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        SpUtils.put(context, "Test4", new Float(4.4));
        Float str = (Float) SpUtils.get(context, "Test4", new Float(1.1));
        Assert.assertEquals("4.4", str.toString());
    }

    @Test
    public void testGetForLong() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        SpUtils.put(context, "Test5", new Long(5));
        Long str = (Long) SpUtils.get(context, "Test5", new Long(4));
        Assert.assertEquals("5", str.toString());
    }

    @Test
    public void testGetForDouble() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        SpUtils.put(context, "Test6", new Double(6.0));
        String str = (String) SpUtils.get(context, "Test6", new Double(4));
        Assert.assertNull(str);
    }

    @Test
    public void testGetList() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        List<String> list = new ArrayList();
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        SpUtils.putList(context,list,"Test1234");
        List<String> listA = SpUtils.getList(context,"Test1234");
        String srt = "";
        for(String a: listA){
            srt= srt + a;
        }
        Assert.assertEquals("1234",srt);
    }

    @Test
    public void testGetMap() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        Map<String,String> map = new HashMap<>();
        map.put("Test1","aaa");
        SpUtils.putMap(context,map,"TestMap");
        Map<String,String> mapA = SpUtils.getMap(context,"TestMap");
        Assert.assertEquals("aaa",mapA.get("Test1"));
    }

    @Test
    public void testRemove() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        SpUtils.put(context, "Test1", "Test1");
        SpUtils.remove(context,"Test1");
        String str = (String) SpUtils.get(context, "Test1", "T");
        Assert.assertEquals("T",str);
    }

    @Test
    public void testClear() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        SpUtils.clear(context);
        Assert.assertTrue(true);
    }

    @Test
    public void testContains() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        SpUtils.put(context, "Test1", "Test1");
        Assert.assertTrue(SpUtils.contains(context,"Test1"));
    }

    @Test
    public void testGetAll() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        SpUtils.put(context, "Test1", "Test1");
        Map<String,?> map = SpUtils.getAll(context);
        Assert.assertNotNull(map.get("Test1"));
    }
}